import 'package:flutter/material.dart';
import 'package:flutter_demo1/http/dao/login_dao.dart';
import 'package:flutter_demo1/navigator/hi_navigator.dart';
import 'package:flutter_demo1/util/string_util.dart';
import 'package:flutter_demo1/widget/app_bar.dart';
import 'package:flutter_demo1/widget/login_effect.dart';
import 'package:flutter_demo1/widget/login_input.dart';

class RegistrationPage extends StatefulWidget {
  const RegistrationPage({super.key});

  @override
  State<RegistrationPage> createState() => _RegistrationPageState();
}

class _RegistrationPageState extends State<RegistrationPage> {
  bool protect = false;
  bool loginEnable = false;
  String? userName;
  String? password;
  String? rePassword;
  String? imoocId;
  String? orderId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(
        "注册",
        rightTitle: "登录",
        rightButtonClick: () {
          HiNavigator.getInstance().onJumpTo(RouteStatus.login);
        },
      ),
      body: Container(
        child: ListView(
          children: [
            LoginEffect(protect: protect),
            LoginInput(
              "用户名",
              "请输入用户名",
              onChanged: (text) {
                userName = text;
                _checkInput();
              },
            ),
            LoginInput(
              "密码",
              "请输入密码",
              obscureText: true,
              onChanged: (text) {
                password = text;
                _checkInput();
              },
              focusChanged: (focus) {
                setState(() {
                  protect = focus;
                });
              },
            ),
            LoginInput(
              "确认密码",
              "请再次输入密码",
              obscureText: true,
              lineStretch: true,
              onChanged: (text) {
                rePassword = text;
                _checkInput();
              },
              focusChanged: (focus) {
                setState(() {
                  protect = focus;
                });
              },
            ),
            LoginInput(
              "慕课网ID",
              "请输入你的慕课网Id",
              keyboardType: TextInputType.number,
              onChanged: (text) {
                imoocId = text;
                _checkInput();
              },
            ),
            LoginInput(
              "课程订单号",
              "请输入课程订单号",
              keyboardType: TextInputType.number,
              lineStretch: true,
              onChanged: (text) {
                orderId = text;
                _checkInput();
              },
            ),
            Padding(
              padding: EdgeInsets.only(left: 20, top: 20, right: 20),
              child: _loginButton(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _loginButton() {
    return InkWell(
      onTap: () {
        _checkParams();
      },
      child: Text("注册"),
    );
  }

  void _checkInput() {
    bool enable;
    if (isNotEmpty(userName) &&
        isNotEmpty(password) &&
        isNotEmpty(rePassword) &&
        isNotEmpty(imoocId) &&
        isNotEmpty(orderId)) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      loginEnable = enable;
    });
  }

  void _checkParams() {
    String? tips;
    if (password != rePassword) {
      tips = "两次密码不一致";
    }
    if (orderId == null || orderId!.length < 4) {
      tips = "订单号长度不够四位";
    }
    if (null != tips) {
      print("fdas");
      return;
    }
    _send();
  }

  void _send() async {
    var result = await LoginDao.registration(
      userName!,
      password!,
      imoocId!,
      orderId!,
    );
    print("result:::$result");
    HiNavigator.getInstance().onJumpTo(RouteStatus.login);
  }
}
